package com.prim.update.impl;

import android.content.Context;

import com.prim.update.callback.DownloadCallback;
import com.prim.update.interfaces.IUpdateDownloader;
import com.prim.update.listener.OnDownloadListener;
import com.prim.update.model.Progress;
import com.prim.update.service.UpdateDownloadService;
import com.prim.update.state.StateType;
import com.prim.update.state.UpdateState;

import java.lang.ref.WeakReference;

/**
 * ================================================
 * 作    者：linksus
 * 版    本：1.0
 * 创建日期：3/5 0005
 * 描    述：下载的实现者
 * 修订历史：
 * ================================================
 */
public class UpdateDownloader implements IUpdateDownloader, DownloadCallback {
    private WeakReference<Context> mContext;

    private UpdateExplorer explorer;

    private OnDownloadListener listener;

    private static final String TAG = "UpdateDownloader";

    UpdateDownloader(Context mContext) {
        this.mContext = new WeakReference<>(mContext);
    }

    @Override
    public void download(OnDownloadListener listener, UpdateExplorer explorer, String url, String path) {
        if (UpdateState.getState() != StateType.DOWNLOAD) {
            this.explorer = explorer;
            this.listener = listener;
            UpdateDownloadService.startActionFoo(mContext.get(), this, url, path);
            UpdateState.setState(StateType.DOWNLOAD);
        }
    }

    @Override
    public void onStart(String tag) {
        if (listener != null && explorer.isShow())
            listener.onStart();
    }

    @Override
    public void onProgress(Progress progress) {
        if (listener != null && explorer.isShow())
            listener.onProgress(progress);
    }

    @Override
    public void onError(String msg, String tag) {
        if (listener != null && explorer.isShow())
            listener.onError(msg);
    }

    @Override
    public void onCancle(String tag) {

    }

    @Override
    public void onFinish(String tag) {
        if (listener != null && explorer.isShow())
            listener.onFinish();
    }
}
